package com.uniedp.modules.login;

import com.uniedp.core.exception.NewSailException;
import com.uniedp.core.utils.StringUtils;
import com.uniedp.modules.security.utils.DateHelper;
import com.uniedp.modules.security.utils.EncryptUtil;

import javax.servlet.http.HttpServletRequest;

/**
 * @Package: com.uniedp.modules.login
 * @ClassName: FrLoginCheck
 * @Description: 帆软登录校验
 * @Author: YJ
 * @Version: 1.0
 */
public class FrLoginCheck {

    /**
     * 帆软 登录获取token校验
     *
     * @param request
     * @throws Exception
     */
    public static void check(HttpServletRequest request) throws Exception {
        String key = "_uniedp_";
        String username = request.getParameter("username");
        String stamp = request.getParameter("_stamp");
        String aes = request.getParameter("_aes");
        if (StringUtils.isBlank(username)
                || StringUtils.isBlank(stamp)
                || StringUtils.isBlank(aes)) {
            throw new NewSailException("params is null");
        }
        String time_8 = stamp.substring(stamp.length() - 8, stamp.length());
        String token2 = EncryptUtil.encode(username + time_8, key);

        if (!DateHelper.check(DateHelper.stampToDate(stamp))) {
            throw new NewSailException("time is invalid");
        }
        if (!token2.equals(aes)) {
            throw new NewSailException("token is invalid");
        }

    }
}
